// pages/waybill/waybill.js
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    courierType: "send",                        // 查询快递类型
    sendCount: "123",                           // 寄出快递数量
    receiveCount: "21",                         // 收到快递数量   
    courierList: [],
    keywords: '',       //查询条件
    pagination: {        //页码
      current_page: 1,    //当前页码
      page_size: 10,     //每页条数
      page_count: 0,    //总页数
      total_count: 0      //总条数 
    },
    listHeight: 0,
    flag_del: true,      //删除标识
    source: "",
    sourceType: "",
  },

  /**
   * 跳转详情
   * @param {*} options 
   */
  toDetail: function (e) {
    console.log('to_detail', e);
    let logistics_id = e.currentTarget.dataset.item.logistics_id
    wx.navigateTo({
      url: '/pages/waybillDetail/waybillDetail?logistics_id=' + logistics_id,
    })
  },
  /**
   * 复制运单号
   */
  copyLogisticsNo(e) {
    let logistics_no = e.currentTarget.dataset.logisticsno;
    wx.setClipboardData({
      data: logistics_no
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      source: options.source,
      sourceType: options.type,
    })
    this.orderList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    wx.getSystemInfo({
      complete: (res) => {
        const query = wx.createSelectorQuery()
        query.select('.filter-area').boundingClientRect()
        query.selectViewport().scrollOffset()
        query.exec(function (r) {
          that.setData({
            listHeight: res.windowHeight - r[0].height
          })
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log('come up');
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (parseInt(this.data.pagination.current_page) >= parseInt(this.data.pagination.page_count)) return;
    this.orderList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 获取订单列表 
   */
  orderList: function () {
    let that = this;
    wx.$api.freeList({
    
    }).then(res => {
      if (res.data.code != 200) {
        wx.$Toast(res.data.msg);
        return;
      }
      this.setData({
        courierList: res.data.data.list,
        pagination: res.data.data.pagination
      });

    })
  },
  /**
    * 展示删除面板
    */
  showDeletePanel: function (e) {
    let that = this;
    if (!that.data.flag_del) return;
    Dialog.confirm({
      title: '',
      message: '确定删除该费用数据？',
      asyncClose: true,
      className: 'btnWidth'
    }).then(() => {
      that.setData({
        flag_del: false
      });
      wx.$api.deleteFeelist({ id: e.currentTarget.dataset.id }).then(res => {
        Dialog.close();
        console.log('ressss', res.data);
        if (parseInt(res.data.code) != 200) {
          wx.$Toast('fasfasfas');
          return;
        }
        that.orderList();
        that.setData({
          flag_del: true
        });
      })
    }).catch(() => {
      Dialog.close();
    });
  },

  /**
   * 查询运单
   */
  onChange: function (e) {
    this.setData({
      keywords: e.detail
    })
  },
  toAdd: function (e) {
    let free = e.currentTarget.dataset.item || {};
    console.log('free', free);
    wx.navigateTo({
      url: '/pages/expenseAdd/expenseAdd?status_name=' + (free.feetype || "")
        + "&free_status=" + (free.fee || "")
        + "&id=" + (free.id || "")
        + "&remarks=" + (free.feeinfo || "")
        + "&type=" + this.data.sourceType
    })
  },


})